package com.travelsky.airchina.boarding.util;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;


public class PageViewUtil {

	/**
	 * 分页器的页面展示
	 * 
	 * @param out
	 * @param pageUtil
	 * @param tab_width
	 * @param formName
	 * @param formAction
	 * @param formTarget
	 * @param isFront
	 *            当在前台(非系统管理)使用此方法时为true,在系统管理时为false
	 * @throws IOException
	 */
	public static final void printPage(JspWriter out, PageUtil pageUtil,
			String tab_width, String formName, String formAction,
			String formTarget, boolean isFront) throws IOException {
		StringBuffer sb = new StringBuffer();
		sb.append("<script type=\"text/javascript\" language=\"JavaScript\">");
		sb.append("function list(curPage){");
		sb.append("document.getElementById(\""+formName + "\").action=\"" + formAction + "\";");
		sb.append("document.getElementById(\""+formName + "\").target=\"" + formTarget + "\";");
		sb.append("document.getElementById(\"curPage\").value=curPage;");
		sb.append("document.getElementById(\""+formName + "\").submit();");
		sb.append("}");
		sb.append("function changePageRow(){");
		sb.append("document.getElementById(\"curPage\").value=\'1\';");
		sb.append("document.getElementById(\""+formName + "\").action='" + formAction + "';");
		sb.append("document.getElementById(\""+formName + "\").submit();");
		sb.append("}");
		sb.append("function goToSelectedPage(){");
		sb.append("document.getElementById(\"curPage\").value=document.getElementById(\"gotoPage\").value;");
		sb.append("document.getElementById(\""+formName + "\").action='" + formAction + "';");
		sb.append("document.getElementById(\""+formName + "\").submit();");
		sb.append("}");
		sb.append("</script>");
		if (false == isFront) {
			sb.append("<table width=\""
					+ tab_width
					+ "\"  border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#A4E3EE\">");
	        sb.append("<tr bgcolor=\"#C5E3EE\">");
		}else{
			sb.append("<table width=\""
					+ tab_width
					+ "\"  border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#A4E3EE\">");
	        sb.append("<tr >");
		}
		sb.append("<td width=\"58%\">");
		sb.append("&nbsp;共&nbsp;<strong>" + pageUtil.getTotalRow()
				+ "</strong>&nbsp;项&nbsp;&nbsp;");
		sb.append("这是第&nbsp;<strong>" + pageUtil.getStartnum()
				+ "</strong>&nbsp;到&nbsp;<strong>" + pageUtil.getEndnum()
				+ "</strong>&nbsp;项&nbsp; &nbsp;&nbsp;");
		sb.append("共&nbsp;<strong>" + pageUtil.getTotalPage()
				+ "</strong>&nbsp;页&nbsp;");
		sb.append("这是第&nbsp;<strong>" + pageUtil.getCurPage()
				+ "</strong>&nbsp;页&nbsp;");
		if (false == isFront) {
			sb.append("每页显示");
			sb.append("<select name=\"perPageRow\" id=\"perPageRow\" onChange=\"changePageRow()\">");
			sb.append("<option value=\"2\"  "
					+ ((pageUtil.getPerPageRow() == 2) ? "selected" : "")
					+ ">2</option>");
			sb.append("<option value=\"10\"  "
					+ ((pageUtil.getPerPageRow() == 10) ? "selected" : "")
					+ ">10</option>");
			sb.append("<option value=\"20\"  "
					+ ((pageUtil.getPerPageRow() == 20) ? "selected" : "")
					+ ">20</option>");
			sb.append("<option value=\"40\"  "
					+ ((pageUtil.getPerPageRow() == 40) ? "selected" : "")
					+ ">40</option>");
			sb.append("</select>");
			sb.append("项</td>");
		}
		sb.append("<td width=\"42%\"><div align=\"right\">");
		if (pageUtil.getCurPage() == pageUtil.getFirstPage()) {
			sb.append("首&nbsp;页&nbsp;");
			sb.append("上&nbsp;页&nbsp;");
		} else {
			sb.append("<a style='text-decoration:underline' href=\"javascript:list(\'1\');\" target=\""
					+ formTarget + "\">首&nbsp;页</a>&nbsp;");
			sb.append("<a style='text-decoration:underline' href=\"javascript:list(\'" + pageUtil.getPrePage()
					+ "');\" target=\"" + formTarget
					+ "\">上&nbsp;页</a>&nbsp;&nbsp;");
		}
		if (pageUtil.getCurPage() == pageUtil.getTotalPage()) {
			sb.append("下&nbsp;页&nbsp;&nbsp;");
			sb.append("尾&nbsp;页&nbsp;&nbsp;");
		} else {
			sb.append("<a style='text-decoration:underline' href=\"javascript:list('" + pageUtil.getNextPage()
					+ "');\" target=\"" + formTarget + "\">下&nbsp;页</a>&nbsp;");
			sb.append("<a style='text-decoration:underline' href=\"javascript:list('" + pageUtil.getTotalPage()
					+ "');\" target=\"" + formTarget + "\">尾&nbsp;页</a>&nbsp;");
		}
		sb.append("跳转到");
		sb.append("<select name=\"gotoPage\" id=\"gotoPage\" onChange=\"goToSelectedPage()\">");
		for (int i = 1; i < pageUtil.getTotalPage() + 1; i++) {
			sb.append("<option value=\"" + i + "\"  "
					+ ((pageUtil.getCurPage() == i) ? "selected" : "") + ">"
					+ i + "</option>");
		}
		sb.append("</select>");
		sb.append("<input name=\"curPage\" id=\"curPage\" type=\"hidden\" value=\""
				+ pageUtil.getCurPage() + "\">");
		sb.append("页</div></td>");
		sb.append("</tr>");
		sb.append("</table>");
		out.print(sb.toString());
	}
}